# include <stdio.h>
# include <cuda_runtime.h>

bool init_cuda()
{
    int count = 0;
    cudaGetDeviceCount(&count); // 获取GPU数量
    if (count == 0)
    {
        printf("没有找到 GPU 显卡设备\n");
        return false;
    }

    int i;
    for (i = 0; i < count; i++) {
        cudaDeviceProp prop;
        cudaGetDeviceProperties(&prop, i);
        printf("第%d块显卡的信息:\n", i);
        printf("  设备名称: %s\n", prop.name);
        printf("  总显存: %ldMB\n", prop.totalGlobalMem / (1024 * 1024));
        printf("  核心数: %d\n", prop.multiProcessorCount);
        printf("  计算能力: %d.%d\n", prop.major, prop.minor);
        printf("  最大线程数: %d\n", prop.maxThreadsPerBlock);
        printf("  最大线程维度: (%d, %d, %d)\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
        printf("  最大网格维度: (%d, %d, %d)\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
        printf("  常规内存: %ldMB\n", prop.totalConstMem / (1024 * 1024));
        printf("  共享内存: %ldKB\n", prop.sharedMemPerBlock / 1024);
        printf("  常规内存对齐: %ld\n", prop.memPitch);
        printf("  线程块对齐: %ld\n", prop.textureAlignment);
        printf("  线程块共享内存: %ldKB\n", prop.sharedMemPerBlock / 1024);
        printf("  线程块最大寄存器数: %d\n", prop.regsPerBlock);
        printf("  线程块最大线程数: %d\n", prop.maxThreadsPerBlock);
        if (prop.major >= 2) {
            printf("  支持动态并行计算\n");
            break;
        }
    }

    if (count == i)
    {
        fprintf(stderr, "没有找到支持动态并行计算的 GPU 显卡设备\n");
        return false;
    }
    cudaSetDevice(i);

    return true;
}

int main()
{
    if (init_cuda() == false)
    {
        return 1;
    }
    printf("初始化 CUDA 成功\n");

    return 0;
}